package edu.whut;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.SortedMap;

public class test {
    public static void main(String[] args) {

        ArrayList<Student> list = new ArrayList<>();
        Student stu1 = new Student("周琦","111",25,"2003-12-11");
        list.add(stu1);
        Scanner sc = new Scanner(System.in);
        while(true){
        System.out.println("------欢迎来到学生管理系统------------------");
        System.out.println("1.添加学生");
        System.out.println("2.删除学生");
        System.out.println("3.修改学生");
        System.out.println("4.查看学生");
        System.out.println("5.退出");
            int choice = sc.nextInt();
            switch (choice){
                case 1:
                    addStudent(list);
                    break;
                case 2:
                    deleteStudent(list);
                    break;
                case 3:
                    changeStudent(list);
                    break;
                case 4:
                    findStudent(list);
                    break;
                case 5:
                    System.out.println("拜拜！");
                    System.exit(0);//终止正在运行的JVM虚拟机
                    break;
            }
        }
    }

    /**
     * 查找学号在集合中的位置
     */
    private static int getIndex(ArrayList<Student> list,String str){
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if(stu.getStudentid().equals(str)){
                return i;
            }
        }
        return -1;
    }
    private static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入学号：");
        String id = sc.next();
        int index = getIndex(list,id);
        if (index != -1){
            System.out.println("学号重复！");
        }else {
            System.out.println("输入姓名：");
            String name = sc.next();
            System.out.println("输入年龄：");
            int age = sc.nextInt();
            System.out.println("输入生日：");
            String birthday = sc.next();
            Student stu = new Student(id,name,age,birthday);
            list.add(stu);
            System.out.println("增加成功！");
        }

    }

    /**
     *删除学生
     */
    private static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入要删除的学生学号：");
        String str = sc.next();
        int index = getIndex(list,str);
        if(index==-1){
            System.out.println("查无此人！");
        }else{
            list.remove(index);
            System.out.println("删除成功！");
        }
    }

    /**
     *修改学生
     */
    private static void changeStudent(ArrayList<Student> list) {
        System.out.println("请输入要修改的学号：");
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int index = getIndex(list,str);
        if(index == -1){
            System.out.println("查无此人！");
        }else{
            System.out.println("输入新姓名：");
            String name = sc.next();
            System.out.println("输入新年龄：");
            int age = sc.nextInt();
            System.out.println("输入新生日：");
            String birthday = sc.next();
            Student stu = new Student(str,name,age,birthday);
            list.set(index,stu);
            System.out.println("修改成功！");
        }
    }

    /**
     * 查看学生
     */
    private static void findStudent(ArrayList<Student> list) {
        if(list.size()==0){
            System.out.println("没有学生！");
        }else{
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                System.out.println(stu.getStudentid()+"--"+stu.getName()+"--"+stu.getBirthday()+"--"+stu.getAge());
                
            }
        }
    }
}
